import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/pages/site/site_setting/site_setting_base_info/widgets/item_widget.dart';
import '../../../../components/app_bar/app_bar_widget.dart';
import '../../../../components/gaps.dart';
import '../../../../components/yt_button.dart';
import '../../../../components/yt_text_style.dart';
import '../../../../resources/str_res_keys.dart';
import '../../../../styles/styles.dart';
import '../access_device/views/infoHeaderView.dart';
import 'access_device_two_controller.dart';

enum DeviceType {
  energy, //储能
  electric, //电表
  inverter, //逆变器
  emsSn, //emsSn
}

class AccessDeviceTwoPage extends GetView<AccessDeviceTwoController> {
  AccessDeviceTwoPage({Key? key}) : super(key: key);
  @override
  final controller = Get.put(AccessDeviceTwoController());
  final BrnFormItemConfig theme = BrnFormItemConfig(
      formPadding: EdgeInsets.only(right: 16.w, top: 10.h, bottom: 10.h));

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: () => FocusScope.of(context).unfocus(),
        child: Scaffold(
          backgroundColor: Colour.backgroundColor,
          resizeToAvoidBottomInset: true,
          appBar: AppBarWidget.withRightActions(ResKey.addNewDevice.tr),
          body: GetBuilder<AccessDeviceTwoController>(
            builder: (controller) => Container(
              color: Colour.backgroundColor,
              child: Column(
                children: [
                  Expanded(
                    child: ListView(
                        padding: EdgeInsets.fromLTRB(0, 10.h, 0, 50.h),
                        children: [
                          InfoHeaderView(controller.type),
                          Gaps.vGap10,
                          Container(
                            clipBehavior: Clip.hardEdge,
                            margin: const EdgeInsets.only(left: 16, right: 16),
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(8),
                              color: Colors.white,
                            ),
                            padding: EdgeInsets.only(
                                left: 16.w,
                                right: 16.w,
                                top: 10,
                                bottom: 10),
                            child: Text(
                              ResKey.parameterSets.tr,
                              style: ytTextStyle(
                                  fontSize: 18, fontWeight: FontWeight.w500),
                            ),
                          ),
                          Gaps.vGap10,
                          //储能模块
                          _energyWidget(),
                          _inverterWidget(),
                          _electricWidget(),
                          _emsSnWidget()
                        ]),
                  ),
                  Align(
                    alignment: Alignment.bottomCenter,
                    child: Container(
                      color: Colour.white,
                      margin: EdgeInsets.only(top: 10.h),
                      padding: EdgeInsets.symmetric(
                          horizontal: 16.w, vertical: 16.h),
                      child: SafeArea(
                        child: YtButton(
                          enabled: controller.btnCanClick,
                          color: controller.btnCanClick
                              ? Colour.primaryColor
                              : Theme.of(context).primaryColorLight,
                          text: ResKey.siteDeviceAccessNext.tr,
                          onPress: () => controller.gotoNext(),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ));
  }

  ///储能只输入emsSn视图
  Widget _emsSnWidget() {
    return Visibility(
      visible: controller.deviceType == DeviceType.emsSn,
      child: Container(
        clipBehavior: Clip.hardEdge,
        margin: const EdgeInsets.only(left: 16, right: 16),
    decoration: BoxDecoration(
    borderRadius: BorderRadius.circular(8),
    color: Colors.white,
    ),
    child:Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
              height: 50.h,
              // color: Colors.red,
              alignment: Alignment.center,
              // margin: const EdgeInsets.only(left: 16),
              child: ItemWidget(
                ResKey.siteDeviceAccessEMSSNNumber.tr,
                controller.controllerList[0],
                inputTextColor: Colour.mainTextColor,
                titleColor: Colour.mainTextColor,
                titleFont: 16,
                inputTextFont: 16,
                isShowRight: false,
                isCanEdit: true,
                isRequired: false,
                isShowDivider: true,
                // hintString: ResKey.pleSelect.tr,
                textChangeCallBack: (text) {
                  controller.checkEmsSnCommitStatus();
                },
              )
              // BrnTextInputFormItem(
              //   title: 'EMS SN号',
              //   onChanged: (value) {
              //     controller.emsSn = value;
              //     controller.btnCanClick = controller.emsSn.isNotEmpty;
              //     controller.update();
              //   },
              //   isRequire: false,
              //   themeData: theme,
              // ),
              ),
        ],
      )),
    );
  }

  ///储能视图
  Widget _energyWidget() {
    return Visibility(
      visible: controller.deviceType == DeviceType.energy,
      child: Container(
        clipBehavior: Clip.hardEdge,
        margin: const EdgeInsets.only(left: 16, right: 16),
    decoration: BoxDecoration(
    borderRadius: BorderRadius.circular(8),
    color: Colors.white,
    ),
    child:Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
              height: 50.h,
              // color: Colors.red,
              alignment: Alignment.center,
              margin: const EdgeInsets.only(left: 16),
              child: ItemWidget(
                controller.titleList[0],
                controller.controllerList[0],
                inputTextColor: Colour.mainTextColor,
                titleColor: Colour.mainTextColor,
                titleFont: 16,
                inputTextFont: 16,
                isShowRight: false,
                isCanEdit: true,
                isRequired: false,
                isShowDivider: true,
                // hintString: ResKey.pleSelect.tr,
                textChangeCallBack: (text) {
                  controller.btnCanClick =
                      controller.emsNameController.text.isNotEmpty &&
                          controller.emsPwdController.text.isNotEmpty;
                  controller.update();
                },
              )
              //   BrnTextInputFormItem(
              //   title: ResKey.siteDeviceAccessEMSName.tr,
              //   onChanged: (value) {
              //     controller.emsName = value;
              //     controller.btnCanClick = controller.emsName.isNotEmpty &&
              //         controller.emsPwd.isNotEmpty;
              //     controller.update();
              //   },
              //   isRequire: false,
              //   themeData: theme,
              // ),
              ),
          const Divider(
            color: Colour.dividerColor,
          ),
          Container(
              height: 50.h,
              // color: Colors.red,
              alignment: Alignment.center,
              margin: const EdgeInsets.only(left: 16),
              child: ItemWidget(
                controller.titleList[1],
                controller.controllerList[1],
                inputTextColor: Colour.mainTextColor,
                titleColor: Colour.mainTextColor,
                titleFont: 16,
                inputTextFont: 16,
                isShowRight: false,
                isCanEdit: true,
                isRequired: false,
                isShowDivider: true,
                // hintString: ResKey.pleSelect.tr,
                textChangeCallBack: (text) {
                  controller.btnCanClick =
                      controller.emsNameController.text.isNotEmpty &&
                          controller.emsPwdController.text.isNotEmpty;
                  controller.update();
                },
              )
              // BrnTextInputFormItem(
              //   title: ResKey.siteDeviceAccessEMSPwd.tr,
              //   onChanged: (value) {
              //     controller.emsPwd = value;
              //     controller.btnCanClick = controller.emsName.isNotEmpty &&
              //         controller.emsPwd.isNotEmpty;
              //     controller.update();
              //   },
              //   isRequire: false,
              //   themeData: theme,
              // ),
              ),
        ],
      )),
    );
  }

  ///逆变器视图
  Widget _inverterWidget() {
    return Visibility(
        visible: controller.deviceType == DeviceType.inverter,
        child: Container(
            clipBehavior: Clip.hardEdge,
            margin: const EdgeInsets.only(left: 16, right: 16),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(8),
              color: Colors.white,
            ),
            height: 50.h,
            // color: Colors.red,
            alignment: Alignment.center,
            // margin: const EdgeInsets.only(left: 16),
            child: ItemWidget(
              ResKey.thirdPartySite.tr,
              controller.threeSiteController,
              inputTextColor: Colour.mainTextColor,
              titleColor: Colour.mainTextColor,
              titleFont: 16,
              inputTextFont: 16,
              isShowRight: true,
              isCanEdit: false,
              isRequired: false,
              isShowDivider: false,
              hintString: ResKey.pleSelect.tr,
              clickCallBack: () {
                controller.chooseSite();
              },
            )
            // BrnTextSelectFormItem(
            //   title: ResKey.thirdPartySite.tr,
            //   onTap: () => controller.chooseSite(),
            //   value: controller.threePartModel.siteName ?? '',
            //   isRequire: false,
            //   themeData: theme,
            // ),
            ));
  }

  ///电表视图
  Widget _electricWidget() {
    return Visibility(
        visible: controller.deviceType == DeviceType.electric,
        child: Container(
        clipBehavior: Clip.hardEdge,
        margin: const EdgeInsets.only(left: 16, right: 16),
    decoration: BoxDecoration(
    borderRadius: BorderRadius.circular(8),
    color: Colors.white,
    ),
    child:Column(children: _getItemsList()
            // [
            //   Container(
            //     height: 50.h,
            //     // color: Colors.red,
            //     alignment: Alignment.center,
            //     margin: const EdgeInsets.only(left: 16),
            //     child: BrnTextInputFormItem(
            //       title: ResKey.siteDeviceAccessEMSName.tr,
            //       onChanged: (value) {
            //         controller.emsName = value;
            //         controller.checkElectricCommitStatus();
            //         controller.update();
            //       },
            //       isRequire: false,
            //       themeData: theme,
            //     ),
            //   ),
            //   const Divider(
            //     color: Colour.dividerColor,
            //   ),
            //   Container(
            //     height: 50.h,
            //     // color: Colors.red,
            //     alignment: Alignment.center,
            //     margin: const EdgeInsets.only(left: 16),
            //     child: BrnTextInputFormItem(
            //       title: ResKey.siteDeviceAccessEMSPwd.tr,
            //       onChanged: (value) {
            //         controller.emsPwd = value;
            //         controller.checkElectricCommitStatus();
            //         controller.update();
            //       },
            //       isRequire: false,
            //       themeData: theme,
            //     ),
            //   ),
            //   const Divider(
            //     color: Colour.dividerColor,
            //   ),
            //   Container(
            //     height: 50.h,
            //     // color: Colors.red,
            //     alignment: Alignment.center,
            //     margin: const EdgeInsets.only(left: 16),
            //     child: BrnTextInputFormItem(
            //       title: ResKey.siteDeviceAccessElectricity.tr,
            //       onChanged: (value) {
            //         controller.electricityChangeRate = value;
            //         controller.checkElectricCommitStatus();
            //         controller.update();
            //       },
            //       isRequire: false,
            //       themeData: theme,
            //     ),
            //   ),
            //   const Divider(
            //     color: Colour.dividerColor,
            //   ),
            //   Container(
            //     height: 50.h,
            //     // color: Colors.red,
            //     alignment: Alignment.center,
            //     margin: const EdgeInsets.only(left: 16),
            //     child: BrnTextInputFormItem(
            //       title: ResKey.siteDeviceAccessVoltage.tr,
            //       onChanged: (value) {
            //         controller.voltageChangeRate = value;
            //         controller.checkElectricCommitStatus();
            //         controller.update();
            //       },
            //       isRequire: false,
            //       themeData: theme,
            //     ),
            //   ),
            //   const Divider(
            //     color: Colour.dividerColor,
            //   ),
            //   Container(
            //     height: 50.h,
            //     // color: Colors.red,
            //     alignment: Alignment.center,
            //     margin: const EdgeInsets.only(left: 16),
            //     child: BrnTextInputFormItem(
            //       title: ResKey.siteDeviceAccessEnergy.tr,
            //       onChanged: (value) {
            //         controller.electricChangeRate = value;
            //         controller.checkElectricCommitStatus();
            //         controller.update();
            //       },
            //       isRequire: false,
            //       themeData: theme,
            //     ),
            //   ),
            //   const Divider(
            //     color: Colour.dividerColor,
            //   ),
            //   Container(
            //     height: 50.h,
            //     // color: Colors.red,
            //     alignment: Alignment.center,
            //     margin: const EdgeInsets.only(left: 16),
            //     child: BrnTextInputFormItem(
            //       title: ResKey.siteDeviceAccessPower.tr,
            //       onChanged: (value) {
            //         controller.powerChangeRate = value;
            //         controller.checkElectricCommitStatus();
            //         controller.update();
            //       },
            //       isRequire: false,
            //       themeData: theme,
            //     ),
            //   ),
            //   const Divider(
            //     color: Colour.dividerColor,
            //   ),
            //   Container(
            //     height: 50.h,
            //     // color: Colors.red,
            //     alignment: Alignment.center,
            //     margin: const EdgeInsets.only(left: 16),
            //     child: BrnTextSelectFormItem(
            //       title: ResKey.siteDeviceAccessRelated.tr,
            //       onTap: () => controller.chooseRelatedDevice(),
            //       value: controller.relationDevice,
            //       isRequire: false,
            //       themeData: theme,
            //     ),
            //   ),
            //
            // ],
            )));
  }

  List<Widget> _getItemsList() {
    List<Widget> list = [];
    for (int i = 0; i < controller.titleList.length; i++) {
      var item = ItemWidget(
        controller.titleList[i],
        controller.controllerList[i],
        inputTextColor: Colour.mainTextColor,
        titleColor: Colour.mainTextColor,
        titleFont: 16,
        inputTextFont: 16,
        isShowRight: false,
        isCanEdit: true,
        isRequired: false,
        isShowDivider: true,
        // hintString: ResKey.pleSelect.tr,
        textChangeCallBack: (text) {
          controller.checkElectricCommitStatus();
        },
      );
      if (i == controller.titleList.length - 1) {
        item = ItemWidget(
          controller.titleList[i],
          controller.controllerList[i],
          inputTextColor: Colour.mainTextColor,
          titleColor: Colour.mainTextColor,
          titleFont: 16,
          inputTextFont: 16,
          isShowRight: true,
          isCanEdit: false,
          isRequired: false,
          isShowDivider: false,
          hintString: ResKey.pleSelect.tr,
          clickCallBack: () {
            controller.chooseRelatedDevice();
          },
        );
      }
      list.add(item);
    }

    return list;
  }
}
